<template>
  <div class="home-view">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="brand-section">
          <h1 class="brand-title">MD2DocKit</h1>
          <p class="brand-subtitle">专业的 Markdown 文档转换工具</p>
        </div>
        
        <!-- 快速统计 -->
        <div class="stats-section">
          <div class="stat-item">
            <div class="stat-number">{{ userStats.totalTasks || 0 }}</div>
            <div class="stat-label">总转换数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ userStats.completedTasks || 0 }}</div>
            <div class="stat-label">已完成</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ userStats.totalSize || '0MB' }}</div>
            <div class="stat-label">处理文件</div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 主要内容区域 -->
    <div class="main-content">
      <div class="content-grid">
        <!-- 文件上传区域 -->
        <div class="upload-section">
          <div class="section-header">
            <h2 class="section-title">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
                <polyline points="7,10 12,15 17,10"/>
                <line x1="12" y1="15" x2="12" y2="3"/>
              </svg>
              开始转换
            </h2>
            <p class="section-description">
              拖拽或选择 Markdown 文件，快速转换为多种格式
            </p>
          </div>
          
          <FileUpload @upload-success="handleUploadSuccess" />
        </div>
        
        <!-- 最近任务 -->
        <div class="tasks-section">
          <div class="section-header">
            <h2 class="section-title">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                <polyline points="14,2 14,8 20,8"/>
                <line x1="16" y1="13" x2="8" y2="13"/>
                <line x1="16" y1="17" x2="8" y2="17"/>
                <polyline points="10,9 9,10 8,9"/>
              </svg>
              最近任务
            </h2>
            <router-link to="/history" class="view-all-link">
              查看全部
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="9,18 15,12 9,6"/>
              </svg>
            </router-link>
          </div>
          
          <TaskList 
            :limit="5" 
            :show-pagination="false"
            @task-updated="handleTaskUpdated"
          />
        </div>
      </div>
      
      <!-- 功能特性 -->
      <div class="features-section">
        <h2 class="features-title">为什么选择 MD2DocKit？</h2>
        
        <div class="features-grid">
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="16,18 22,12 16,6"/>
                <polyline points="8,6 2,12 8,18"/>
              </svg>
            </div>
            <h3 class="feature-title">多格式支持</h3>
            <p class="feature-description">
              支持转换为 Word、PDF、HTML、EPUB 等多种格式，满足不同需求
            </p>
          </div>
          
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <circle cx="12" cy="12" r="10"/>
                <polyline points="12,6 12,12 16,14"/>
              </svg>
            </div>
            <h3 class="feature-title">快速转换</h3>
            <p class="feature-description">
              采用高性能转换引擎，大文件也能在短时间内完成转换
            </p>
          </div>
          
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
              </svg>
            </div>
            <h3 class="feature-title">安全可靠</h3>
            <p class="feature-description">
              文件加密传输，定期自动清理，保护您的隐私和数据安全
            </p>
          </div>
          
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/>
                <circle cx="9" cy="7" r="4"/>
                <path d="M23 21v-2a4 4 0 0 0-3-3.87"/>
                <path d="M16 3.13a4 4 0 0 1 0 7.75"/>
              </svg>
            </div>
            <h3 class="feature-title">团队协作</h3>
            <p class="feature-description">
              支持批量处理和任务分享，提升团队文档处理效率
            </p>
          </div>
        </div>
      </div>
      
      <!-- 使用指南 -->
      <div class="guide-section">
        <h2 class="guide-title">使用指南</h2>
        
        <div class="guide-steps">
          <div class="step-item">
            <div class="step-number">1</div>
            <div class="step-content">
              <h3 class="step-title">上传文件</h3>
              <p class="step-description">
                拖拽或点击选择您的 Markdown 文件，支持 .md、.markdown 格式
              </p>
            </div>
          </div>
          
          <div class="step-item">
            <div class="step-number">2</div>
            <div class="step-content">
              <h3 class="step-title">选择格式</h3>
              <p class="step-description">
                选择目标输出格式，可以同时转换为多种格式
              </p>
            </div>
          </div>
          
          <div class="step-item">
            <div class="step-number">3</div>
            <div class="step-content">
              <h3 class="step-title">开始转换</h3>
              <p class="step-description">
                点击开始转换，系统会自动处理您的文件
              </p>
            </div>
          </div>
          
          <div class="step-item">
            <div class="step-number">4</div>
            <div class="step-content">
              <h3 class="step-title">下载结果</h3>
              <p class="step-description">
                转换完成后，您可以下载转换后的文件
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useTaskStore } from '@/stores/task'
import FileUpload from '@/components/FileUpload.vue'
import TaskList from '@/components/TaskList.vue'

// Stores
const router = useRouter()
const userStore = useUserStore()
const taskStore = useTaskStore()

// 响应式数据
const userStats = ref({
  totalTasks: 0,
  completedTasks: 0,
  totalSize: '0MB'
})

// 方法
const handleUploadSuccess = (taskId: string) => {
  console.log('Upload success:', taskId)
  // 刷新任务列表
  taskStore.fetchTasks()
  // 可以显示成功提示
}

const handleTaskUpdated = () => {
  // 任务更新时刷新统计数据
  loadUserStats()
}

const loadUserStats = async () => {
  try {
    const stats = await userStore.getUserStats()
    userStats.value = stats
  } catch (error) {
    console.error('Failed to load user stats:', error)
  }
}

// 生命周期
onMounted(() => {
  loadUserStats()
  taskStore.fetchTasks()
})
</script>

<style scoped>
.home-view {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 页面头部 */
.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding: 2rem 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand-section {
  text-align: left;
}

.brand-title {
  font-size: 3rem;
  font-weight: 700;
  color: white;
  margin: 0 0 0.5rem 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 1.2rem;
  color: rgba(255, 255, 255, 0.9);
  margin: 0;
}

.stats-section {
  display: flex;
  gap: 2rem;
}

.stat-item {
  text-align: center;
  color: white;
}

.stat-number {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.stat-label {
  font-size: 0.875rem;
  opacity: 0.8;
}

/* 主要内容 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 3rem 2rem;
}

.content-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3rem;
  margin-bottom: 4rem;
}

.section-header {
  margin-bottom: 1.5rem;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1.5rem;
  font-weight: 600;
  color: white;
  margin: 0 0 0.5rem 0;
}

.section-description {
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  line-height: 1.5;
}

.view-all-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 0.875rem;
  transition: all 0.2s ease;
}

.view-all-link:hover {
  color: white;
}

/* 上传和任务区域 */
.upload-section,
.tasks-section {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 2rem;
}

/* 功能特性 */
.features-section {
  margin-bottom: 4rem;
}

.features-title {
  text-align: center;
  font-size: 2.5rem;
  font-weight: 700;
  color: white;
  margin: 0 0 3rem 0;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
}

.feature-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 2rem;
  text-align: center;
  transition: all 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-4px);
  background: rgba(255, 255, 255, 0.15);
}

.feature-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  margin: 0 auto 1.5rem auto;
  color: white;
}

.feature-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: white;
  margin: 0 0 1rem 0;
}

.feature-description {
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.6;
  margin: 0;
}

/* 使用指南 */
.guide-section {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 3rem;
}

.guide-title {
  text-align: center;
  font-size: 2rem;
  font-weight: 700;
  color: white;
  margin: 0 0 3rem 0;
}

.guide-steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
}

.step-item {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
}

.step-number {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  font-weight: 700;
  color: white;
}

.step-content {
  flex: 1;
}

.step-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: white;
  margin: 0 0 0.5rem 0;
}

.step-description {
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.5;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .header-content {
    flex-direction: column;
    gap: 2rem;
    text-align: center;
  }
  
  .content-grid {
    grid-template-columns: 1fr;
    gap: 2rem;
  }
  
  .features-grid {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  }
}

@media (max-width: 768px) {
  .main-content {
    padding: 2rem 1rem;
  }
  
  .brand-title {
    font-size: 2.5rem;
  }
  
  .stats-section {
    gap: 1rem;
  }
  
  .upload-section,
  .tasks-section,
  .guide-section {
    padding: 1.5rem;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
  }
  
  .guide-steps {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .header-content {
    padding: 0 1rem;
  }
  
  .brand-title {
    font-size: 2rem;
  }
  
  .brand-subtitle {
    font-size: 1rem;
  }
  
  .stats-section {
    flex-direction: column;
    gap: 1rem;
  }
  
  .step-item {
    flex-direction: column;
    text-align: center;
    gap: 0.5rem;
  }
}
</style>